package com.aaa.mrcai.service.impl;

import com.aaa.mrcai.AlipayConfig;
import com.aaa.mrcai.pojo.entity.Test;
import com.aaa.mrcai.pojo.vo.ShopSoVO;
import com.aaa.mrcai.result.AjaxResult;
import com.aaa.mrcai.service.OrderAddService;
import com.aaa.mrcai.service.ZfbTestService;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;

/**
 * @ Author: Lining
 * @ Date: 2021/12/16 10:20
 */
@Service
public class ZfbTestSeviceImpl implements ZfbTestService {

    @Autowired
    private ServletContext servletContext;
    @Autowired
    private OrderAddService orderAddService;

    private  String result=null;
    private  String resultQuery=null;
    private JSONObject jsonObjectQ= null;
    private Object o1= null;

    @Override
    public AjaxResult zfbtest(Test test) {
        try {
            //订单号
            System.out.println(test);
            String orderId = test.getSoid();
            //订单总价
            double total = test.getSprice();
            String totals = String.valueOf(total);
            String gname = test.getGname();

            //将test存入liushui
            servletContext.setAttribute("liushui",test);
            //获得初始化的AlipayClient
        AlipayClient alipayClient = new DefaultAlipayClient(
                AlipayConfig.gatewayUrl,
                AlipayConfig.app_id,
                AlipayConfig.merchant_private_key, "json",
                AlipayConfig.charset,
                AlipayConfig.alipay_public_key,
                AlipayConfig.sign_type);

        //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(AlipayConfig.return_url);
        alipayRequest.setNotifyUrl(AlipayConfig.notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = new String(orderId.getBytes("ISO-8859-1"),"UTF-8");
        //付款金额，必填
        String total_amount = new String(totals.getBytes("ISO-8859-1"),"UTF-8");
        //订单名称，必填
        String subject = new String(gname);

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        //若想给BizContent增加其他可选请求参数，以增加自定义超时时间参数timeout_express来举例说明
        //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
        //		+ "\"total_amount\":\""+ total_amount +"\","
        //		+ "\"subject\":\""+ subject +"\","
        //		+ "\"body\":\""+ body +"\","
        //		+ "\"timeout_express\":\"10m\","
        //		+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
        //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节

        //请求
        result = alipayClient.pageExecute(alipayRequest).getBody();

        //输出
        }catch (Exception e){
            e.printStackTrace();
        }

        return new AjaxResult(1001,"aksdhf",result);
    }

    @Override
    public AjaxResult queryTest() {
        try {

            System.out.println("*****************************");
            Test test = (Test) servletContext.getAttribute("liushui");
            String liushui = test.getSoid();
            String liushuiId = String.valueOf(liushui);

            System.out.println(liushui);

            //获得初始化的AlipayClient
            AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);

            //设置请求参数
            AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest();

            //商户订单号，商户网站订单系统中唯一订单号
            String out_trade_no = new String(liushuiId);

            alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"}");

            //请求
            resultQuery = alipayClient.execute(alipayRequest).getBody();


            jsonObjectQ = JSON.parseObject(resultQuery);
            JSONObject o = jsonObjectQ.getJSONObject("alipay_trade_query_response");
             o1 = o.get("code");
            System.out.println(o1);


            if ("10000".equals(o1)){
                return new AjaxResult(10000,"成功下单",null);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return new AjaxResult(101,"下单失败",null);
    }

    @Override
    public int addOrder() {
        Test liushui = (Test) servletContext.getAttribute("liushui");
        return orderAddService.updataOrder(liushui);
    }
}
